﻿using MySql.Data.MySqlClient;

using RantopadService.Conn;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace RantopadService.Business
{
    public class RawCfgFileBusiness
    {
       
        public static DataTable getAllRawcfgfile(string wherelike,string typeID)
        {
            string mysql = "SELECT a.* from rawcfgfile  a";
           
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            var dataAdapter = new MySqlDataAdapter(mysql, myconnection);
            var dt = new DataTable();
            dataAdapter.Fill(dt);
            myconnection.Close();

            return dt;
        }

        public static DataTable getRawcfgfile(string strUserId, string strPid)
        {
            string mysql = "SELECT a.* from rawcfgfile  a   where a.userid='{0}' and a.pid='{1}'";
            mysql = string.Format(mysql, strUserId, strPid);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            var dataAdapter = new MySqlDataAdapter(mysql, myconnection);
            var dt = new DataTable();
            dataAdapter.Fill(dt);

            myconnection.Close();
            return dt;
        }



        public static int checkData(string strUserId, string strPid)
        {
            string mysql = "SELECT count(ID) as count from rawcfgfile where userid = '{0}' and pid = '{1}' ";
            mysql = string.Format(mysql, strUserId, strPid);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
            { myconnection.Close(); return 0; }
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }


        public static int delRawcfgfileByID(string id)
        {
            string mysql = "delete from rawcfgfile where ID='{0}'";
            mysql = string.Format(mysql, id);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            
            var count=mycommand.ExecuteNonQuery();

            myconnection.Close();
            return count;
        }



        public static int addRawcfgfile(string  userid,string pid,string cfgfilecontent,string hash)
        {
            //INSERT INTO `rantopad`.`rawcfgfile` (`NAME`, `USER_PSW`, `EMAIL`, `ONLINE`) VALUES ('aaaaa', 'a', 'liang@163.com', '0');
            int count = 0;var myconnection = MyMySqlHelper.GetConn();
                myconnection.Open();
            try
            {
                string mysql = "insert into `rawcfgfile` ( `userid`, `pid`, `cfgfilecontent`, `lastupdatetime`, `hash`) VALUES ('{0}', '{1}', '{2}','{3}','{4}') ";
                mysql = string.Format(mysql,  userid, pid, cfgfilecontent, DateTime.Now.ToString(), hash);
                //创建数据库连接
                
                MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

                count = mycommand.ExecuteNonQuery();
                myconnection.Close();
            }
            catch (Exception e)
            {
                myconnection.Close();
                NetLog.WriteTextLog(System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), e.Message.ToString(), DateTime.Now);
                count = 0;
            }
            

            return count;
        }


        internal static int updateRawcfgfile(string userid, string pid, string cfgfilecontent, string hash)
        {
            int count = 0;//创建数据库连接
                var myconnection = MyMySqlHelper.GetConn();
                myconnection.Open();
            try
            {
                string mysql = "update `rawcfgfile` set `cfgfilecontent`='{0}', `hash`='{1}', lastupdatetime='{4}' where `userid`='{2}' and `pid`='{3}'";
                mysql = string.Format(mysql, cfgfilecontent,hash, userid,pid,DateTime.Now.ToString());
                
                MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

                count = mycommand.ExecuteNonQuery();
                myconnection.Close();
            }
            catch (Exception e)
            {
                myconnection.Close();
                NetLog.WriteTextLog(System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), e.Message.ToString(), DateTime.Now);
                count = 0;
            }


            return count;
        }
     
    }
}